package com.infinitegroup.findmystuff.activities;

import com.infinitegroup.findmystuff.R;
import com.infinitegroup.findmystuff.presenters.LoginPresenter;
import com.infinitegroup.findmystuff.views.LoginView;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 
 * Handles the inputs of the login screen and gives these inputs to the login
 * controller (who actually handles checking with the database) and can advance to
 * home screen, lockout screen, admin home screen, and register screen.
 * 
 * @author Maria
 *
 */
public class LoginActivity extends Activity implements LoginView{
	
	private LoginPresenter presenter;
	
	/**
	 * Handles the start of the activity and sets the listeners for the buttons on the screen.
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		presenter = new LoginPresenter(this);

		Button login = (Button) findViewById(R.id.loginButton);
		Button register = (Button) findViewById(R.id.registerButton);

		login.setOnClickListener(new OnClickListener() {

			/**
			 * Handles the clicking of the login button.
			 */
			public void onClick(View v) {
				onLoginClick();	
			}
		});

		register.setOnClickListener(new OnClickListener() {
			
			/**
			 * Handles the clicking of the register button.
			 */
			public void onClick(View v) {
				onRegisterClick();
			}
		});	
	}
	
	/**
	 * Handles login click by getting the info from the text fields and giving it to the presenter.
	 */
	private void onLoginClick(){
		String username = ((EditText)findViewById(R.id.usernameText)).getText().toString();
		String password = ((EditText)findViewById(R.id.passwordText)).getText().toString();
		System.out.println(username + " " + password);
		presenter.handleLoginClick(username, password);
	}
	
	/**
	 * Handles register click by giving this information to the presenter.
	 */
	private void onRegisterClick(){
		presenter.handleRegisterClick();
	}

	/**
	 * Inflates the menu.
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	/**
	 * Advances the app to the home screen.
	 */
	public void advanceToHome(String username) {
		Intent intent = new Intent(this, HomeActivity.class);
		if (username!=null){
			intent.putExtra("username", username);
		}
		startActivity(intent);
	}

	/**
	 * Displays a message telling the user to retry logging in.
	 */
	public void retry() {
		Toast toast = Toast.makeText(this, "Invalid user name/password combination.", Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	/**
	 * Advances the app to the locked screen.
	 */
	public void advanceToLocked() {
		Intent intent = new Intent(this, LockoutActivity.class);
		startActivity(intent);
	}

	/**
	 * Advances the app to the registration screen.
	 */
	public void advanceToRegistration() {
		Intent intent = new Intent(this, RegistrationActivity.class);
		startActivity(intent);		
	}
	
	/**
	 * Displays inputed text as a popup text message.
	 */
	public void displayMessage(String string) {
		Toast toast = Toast.makeText(this, string, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();		
	}

	/**
	 * Advances the app to the admin home screen.
	 */
	public void advanceToAdminHome(String adminUsername) {
		Intent intent = new Intent(this, AdminHomeActivity.class);
		intent.putExtra("username", adminUsername);
		startActivity(intent);
	}

}
